/***************************************************
This file takes all the graduates in the all_students file,
which have been flagged, and extracts when they were enrolled 
***************************************************/


proc sort data=INPUTS.pseo_oh_odhe_2002_2019a_enr_qpik out=qpik ;
	by dqb_source_id ; 
run;

proc sort data=INPUTS.pseo_oh_odhe_2002_2019a_enr_rsch out=rsch ;
	by dqb_source_id ; 
run;
proc freq data=INPUTS.pseo_oh_odhe_2002_2019a_enr_rsch ;
                    tables OPEID ; 
run;                   
data enrollment (keep = pik degcip_2dig opeid  credithours reportterm 
				reportyear year quarter state cte_flag deglevl_code )
    ;
    
    merge rsch (in=a 	rename=(opeid=opeid_num calendar_year=year 
                        cum_gpa_credit_hours = credithours)) 
          qpik (in=b rename=(qpik_ssn = pik)) ;
    by dqb_source_id ; 

	state = '39' ; 
        opeid_char = put(opeid_num,8.) ;
        opeid = "00" || substr(opeid_char,1,6) ;
                        
	if student_level = "To obtain a certificate" then deglevl_code = '01' ;
	if student_level = "To obtain an associate degree for the job market" then deglevl_code='03' ;
	if student_level = "To obtain an associate degree for transfer" then deglevl_code ='03' ;

    /* need to add qtimes */
		
	if term = "AU" then quarter = 4 ;
	if term = "WI" then quarter = 1 ; 
	if term = "SP" then quarter = 2 ; 
	if term = "SM" then quarter = 3 ; 
	degcip_2dig = substr(major,1,2) ;
    
    if year>=2001 & deglevl_code in ('01','02','03','04') ;
    
    if _N_ < 50 then put year= quarter= ;
run;


proc sort data=enrollment out=enrollmentsort; 
    by pik opeid ; 
run;
    
 proc freq data=enrollmentsort ;
    tables year quarter/missing;
run;   
    
proc sort data=OUTPUTS.all_students_odhe (where=(pik ne '') )
                    out=all (drop=year quarter) ; 
    by pik opeid ; 
run; 
proc contents data=all ; 
run;                
proc contents data=enrollmentsort ;
run;                    
proc print data=enrollmentsort (obs=50)         ;
                    title 'enrollment print';
                    var pik opeid year quarter ; 
run;                   
                    
proc freq data=all ; 
       title 'ALL tabulations' ; 
       tables opeid  ; 
run;                    
                    

data OUTPUTS.enrollment_long_odhe; 
    merge enrollmentsort (in=a) all (in=b) ;
    by pik opeid ; 
    
    if b and a; 
    enroll= a ; 
                    _merge = a + 2*b ; 
    qtime = %qtime(year,quarter,location=datastep) ;
    if _N_ < 50 then put qtime= year= quarter= enroll=; 
run;
                    
proc freq data=OUTPUTS.enrollment_long_odhe;
                    tables enroll/missing;
                    tables graduate/missing;  
                    tables _merge year quarter; 
run;                    

proc sort data=OUTPUTS.enrollment_long_odhe ;
	by pik opeid deglevl_code degcip_2dig qtime ; 
run;

proc sort data=OUTPUTS.enrollment_long_odhe out=sample_flag ; 
    by pik opeid ; 
run;


/****************************
Making enrollment wide file
*****************************/

data OUTPUTS.enrollment_wide_odhe (keep = pik opeid degcip_2dig credithours 
	flag_cte graduate enr61-enr138 state deglevl_code qtime_grad)  ;
	set sample_flag;
	by pik opeid ;
	
	retain enr21-enr138 ;
	array enr{21:138} enr21-enr138;  
	if qtime <= 138 ; 
	if first.opeid then do ; 
        do i = 21 to 138 ; 
            enr{i} = 0 ; 
        end;
    end;
         
    enr{qtime} = enroll ;  
            
    if last.pik then output ; 
run;

proc sort data=OUTPUTS.enrollment_wide_odhe ;
	by pik opeid deglevl_code degcip_2dig ; 
run;
